;(function () {
  const elevator = document.querySelector('.xtx-elevator')
  // console.log(elevator)
  const toTop = document.querySelector('#backTop')
  window.addEventListener('scroll', function () {
    const n = document.documentElement.scrollTop
    // console.log(n)
    elevator.style.opacity = n > 300 ? '1' : '0'
  })

  toTop.addEventListener('click', function () {
    // 回到页面顶部
    // window.scrollTo(0, 0)
    // 要获取当前页面的滚动条纵坐标位置
    document.documentElement.scrollTop = 0
  })
})()
;(function () {
  const list = document.querySelector('.xtx-elevator-list')
  const newGoods = document.querySelector('.xtx_goods_new')
  const popular = document.querySelector('.xtx_goods_popular')
  const brand = document.querySelector('.xtx_goods_brand')
  const topic = document.querySelector('.xtx_goods_topic')

  list.addEventListener('click', function (e) {
    // console.log(e.target.tagName)
    if (e.target.tagName === 'A' && e.target.dataset.name) {
      // console.log(e.target.dataset.name)
      const goodsName = e.target.dataset.name

      switch (goodsName) {
        case 'newGoods':
          window.scrollTo(0, newGoods.offsetTop)
          break
        case 'popular':
          window.scrollTo(0, popular.offsetTop)
          break
        case 'brand':
          window.scrollTo(0, brand.offsetTop)
          break
        case 'topic':
          window.scrollTo(0, topic.offsetTop)
          break
        default:
          alert('出bug了')
      }
    }
  })

  window.addEventListener('scroll', function () {
    const n = document.documentElement.scrollTop

    list.querySelector('a.active') &&
      list.querySelector('a.active').classList.remove('active')
    if (n >= newGoods.offsetTop && n < popular.offsetTop) {
      list.querySelector('a[data-name=newGoods]').classList.add('active')
    } else if (n >= popular.offsetTop && n < brand.offsetTop) {
      list.querySelector('a[data-name=popular]').classList.add('active')
    } else if (n >= brand.offsetTop && n < topic.offsetTop) {
      list.querySelector('a[data-name=brand]').classList.add('active')
    } else if (n >= topic.offsetTop) {
      list.querySelector('a[data-name=topic]').classList.add('active')
    }
  })
})()
